package com.example.shop_2207.register

import android.content.Intent
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2207.R
import com.example.shop_2207.WebviewActivity
import com.example.shop_2207.databinding.ActivityRegisterBinding
import com.example.shop_2207.login.LoginActivity
import com.example.shop_2207.mvvm.BaseActivity
@Route(path = "/app/RegisterActivity")
class RegisterActivity : BaseActivity<ActivityRegisterBinding,RegisterViewModel>() {
    override fun getLayout(): Int = R.layout.activity_register

    override fun getViewModel(): Class<RegisterViewModel> = RegisterViewModel::class.java

    override fun initView() {
        binding.btnReg.setOnClickListener {
            val username = binding.etUsername.text.toString()
            val password = binding.etPassword.text.toString()
            val repassword = binding.etRepassword.text.toString()
            if(username.isEmpty()||password.isEmpty()||repassword.isEmpty()){
                ToastUtils.showLong("用户名密码不能为空")
            }else if(!RegexUtils.isMobileSimple(username)){
                ToastUtils.showLong("用户名格式不正确，应该为手机号")
            }else if(!RegexUtils.isMatch("^[\\w]{6,8}\$",password)){
                ToastUtils.showLong("密码格式不正确，应该为6-8位的数字、字母、_组合")
            }else if(password!=repassword){
                ToastUtils.showLong("两次密码不一致")
            } else{
                viewModel.register(username,password, repassword)
            }
        }
        //富文本
        val builder = SpannableStringBuilder(binding.tv.text.toString())
        builder.setSpan(ForegroundColorSpan(Color.RED),6,12,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        //富文本的点击事件
        val clickableSpan:ClickableSpan = object :ClickableSpan(){
            override fun onClick(widget: View) {
                //ToastUtils.showLong("点击了隐私协议")
                val intent = Intent(this@RegisterActivity,WebviewActivity::class.java)
                intent.putExtra("url","http://www.baidu.com")
                startActivity(intent)
            }
        }
        builder.setSpan(clickableSpan,13,19,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.tv.movementMethod = LinkMovementMethod.getInstance()//一定添加这一行，不然点击事件没有效果
        binding.tv.text = builder
    }

    override fun initData() {
        viewModel.success.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //跳转到登录界面，并将用户名和密码显示在登录界面
              /*  val intent = Intent(this,LoginActivity::class.java)
                intent.putExtra("username",it.data.username)
                intent.putExtra("password",it.data.password)
                startActivity(intent)*/
                ARouter.getInstance().build("/app/LoginActivity").withString("username",it.data.username)
                    .withString("password",it.data.password).navigation()
            }
        }
        viewModel.fail.observe(this){
            ToastUtils.showLong(it)
        }
    }

}